package com.tansun.easycare.rule.bakproject.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tansun.easycare.core.utils.IdGen;
import com.tansun.easycare.rule.bakproject.entity.RuleProjectRes;
import com.tansun.easycare.rule.bakproject.service.RuleProjectCopyService;
import com.tansun.easycare.rule.bakproject.service.RuleProjectResService;
import com.tansun.easycare.rule.datamodel.dao.RuleDatamodelDao;
import com.tansun.easycare.rule.datamodel.dao.RuleDataparamsDao;
import com.tansun.easycare.rule.datamodel.dao.RuleDatasetDao;
import com.tansun.easycare.rule.datamodel.entity.RuleDatamodel;
import com.tansun.easycare.rule.datamodel.entity.RuleDataparams;
import com.tansun.easycare.rule.datamodel.entity.RuleDataset;
import com.tansun.easycare.rule.deployonline.dao.RuleDeploymentDao;
import com.tansun.easycare.rule.deployonline.entity.RuleDeployment;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruleproj.entity.RuleProject;
import com.tansun.easycare.rule.ruleproj.service.RuleProjectService;

@Service
public class RuleProjectCopyServiceImpl implements RuleProjectCopyService {
	
	@Autowired
	private  RuleProjectResService   ruleProjectResService;
	
	@Autowired
	private  RuleProjectService     ruleProjectService;
	
	@Autowired
	private  RuleManageService   ruleManageService;
	
	
    @Autowired
    private RuleDatamodelDao RuleDatamodelDao;
    
    @Autowired
    private  RuleDatasetDao  ruleDatasetDao;
    
    @Autowired
    private  RuleDataparamsDao   ruleDataparamsDao;
    
    @Autowired
    private  RuleDeploymentDao  ruleDeploymentDao;
    

	@Override
	public void copyProject(String projectCode,String newProjectCode, String  newProjectName)  throws  BizException {
		 // 
		  this.ruleProjectResService.projectCopy(projectCode, newProjectName);
		  this.upateProjectRes(projectCode, newProjectCode,"2");
	}
	
	
	@Override
	public  void  upateProjectRes(String  projectCode,String newProjectCode ,String  flag) {
		RuleProjectRes   queryProjectRes  = new  RuleProjectRes();
		queryProjectRes.setSourceProjectcode(projectCode);
		queryProjectRes.setTargetProjectcode(newProjectCode);
		queryProjectRes.setType("00");
		queryProjectRes.setStatus("1");
		List<RuleProjectRes> list =this.ruleProjectResService.findList(queryProjectRes);
		if (list != null && list.size() >0) {
			RuleProjectRes  projectRes = list.get(0);
			projectRes.setStatus(flag);
			this.ruleProjectResService.save(projectRes);			
		}
		
	}
	
	
	@Override
	public  void deleteProjectCopy(String  projectCode) {
		this.ruleProjectResService.deleteProjectCopy(projectCode);
	}
	
	
	@Override
    public   void   createProjectInfo(String projectCode, String newProjectCode)  throws  BizException{
		RuleProjectRes  projectRes  = new  RuleProjectRes();
		projectRes.setTargetProjectcode(newProjectCode);
		projectRes.setStatus("1");
		projectRes.setType("00");
	    List<RuleProjectRes>  list =	 this.ruleProjectResService.findList(projectRes);
	    if (list != null  &&  list.size() >0) {
	    	throw  new  BizException("", "项目正在复制!");
	    }
	    RuleProject  ruleProject   = new  RuleProject();
	    ruleProject.setProjectcode(projectCode);
	    ruleProject.setStatus("1");
	    List<RuleProject>  pList  =  ruleProjectService.findList(ruleProject);
	    if (pList  != null  &&  pList.size() >0) {
	    	RuleProject  tmpProject  = pList.get(0);
	    	RuleProjectRes   ruleProjectRes  = new  RuleProjectRes();
	    	ruleProjectRes.setSourceId(tmpProject.getId());
	    	ruleProjectRes.setTargetId(IdGen.uuid());
	    	ruleProjectRes.setShortTargetId(IdGen.getUuid16());
	    	ruleProjectRes.setType("00");
	    	ruleProjectRes.setSourceProjectcode(projectCode);
	    	ruleProjectRes.setTargetProjectcode(newProjectCode);
	    	ruleProjectRes.setStatus("1");
	    	this.ruleProjectResService.save(ruleProjectRes);  	
	    	
	    	
	    }
	  
	    RuleManage  queryManage  = new  RuleManage();
	    queryManage.setProjectCode(projectCode);
	    
	    List<RuleManage> mList =  this.ruleManageService.findList(queryManage);
	    RuleProjectRes  tmpProjectRes  = null;
	    List<RuleProjectRes>  resList  = new ArrayList<RuleProjectRes>();
	    for (RuleManage  ruleManage : mList) {	    	
	    	tmpProjectRes  = new  RuleProjectRes();
	    	tmpProjectRes.setSourceId(ruleManage.getId());
	    	tmpProjectRes.setTargetId(IdGen.uuid());
	    	tmpProjectRes.setShortTargetId(IdGen.getUuid16());
	    	tmpProjectRes.setSourceProjectcode(projectCode);
	    	tmpProjectRes.setTargetProjectcode(newProjectCode);
	    	tmpProjectRes.setStatus("1");
	    	tmpProjectRes.setType("01");   	
	    	resList.add(tmpProjectRes);
	    }

	    
	    //数据模型	    
	    RuleDatamodel  queryDataModel  = new  RuleDatamodel();
	    queryDataModel.setProjectCode(projectCode);
	    List<RuleDatamodel>  dataList =this.RuleDatamodelDao.findRuleDatamodelList(queryDataModel);
	    for (RuleDatamodel  ruleDatamodel : dataList) {
	    	tmpProjectRes  = new  RuleProjectRes();
	    	tmpProjectRes.setSourceId(ruleDatamodel.getId());
	    	tmpProjectRes.setTargetId(IdGen.uuid());
	    	tmpProjectRes.setShortTargetId(IdGen.getUuid16());
	    	tmpProjectRes.setSourceProjectcode(projectCode);
	    	tmpProjectRes.setTargetProjectcode(newProjectCode);
	    	tmpProjectRes.setStatus("1");
	    	tmpProjectRes.setType("02");   	
	    	resList.add(tmpProjectRes);
	    }
	    //数据集
	    RuleDataset  queryDataset  = new  RuleDataset();
	    queryDataset.setProjectcode(projectCode);
	    List<RuleDataset> setList =this.ruleDatasetDao.findList(queryDataset);
	    for (RuleDataset  ruleDataset  : setList) {
	    	tmpProjectRes  = new  RuleProjectRes();
	    	tmpProjectRes.setSourceId(ruleDataset.getId());
	    	tmpProjectRes.setTargetId(IdGen.uuid());
	    	tmpProjectRes.setShortTargetId(IdGen.getUuid16());
	    	tmpProjectRes.setSourceProjectcode(projectCode);
	    	tmpProjectRes.setTargetProjectcode(newProjectCode);
	    	tmpProjectRes.setStatus("1");
	    	tmpProjectRes.setType("03");   	
	    	resList.add(tmpProjectRes);
	    }
	    
	    // 数据变量
	    RuleDataparams  queryDataparams  = new  RuleDataparams();
	    queryDataparams.setProjectcode(projectCode);
	    List<RuleDataparams> paList = this.ruleDataparamsDao.findList(queryDataparams);
	    for (RuleDataparams ruleDataparam :paList) {
	    	tmpProjectRes  = new  RuleProjectRes();
	    	tmpProjectRes.setSourceId(ruleDataparam.getId());
	    	tmpProjectRes.setTargetId(IdGen.uuid());
	    	tmpProjectRes.setShortTargetId(IdGen.getUuid16());
	    	tmpProjectRes.setSourceProjectcode(projectCode);
	    	tmpProjectRes.setTargetProjectcode(newProjectCode);
	    	tmpProjectRes.setStatus("1");
	    	tmpProjectRes.setType("04");   	
	    	resList.add(tmpProjectRes);
	    }
	    
  
	    //部署信息
	    RuleDeployment  queryDeployment = new  RuleDeployment();
	    queryDataModel.setProjectCode(projectCode);
	    List<RuleDeployment>  depList =this.ruleDeploymentDao.findList(queryDeployment);
	    for (RuleDeployment  deployment :  depList) {
	    	tmpProjectRes  = new  RuleProjectRes();
	    	tmpProjectRes.setSourceId(deployment.getId());
	    	tmpProjectRes.setTargetId(IdGen.uuid());
	    	tmpProjectRes.setShortTargetId(IdGen.getUuid16());
	    	tmpProjectRes.setSourceProjectcode(projectCode);
	    	tmpProjectRes.setTargetProjectcode(newProjectCode);
	    	tmpProjectRes.setStatus("1");
	    	tmpProjectRes.setType("05");   	
	    	resList.add(tmpProjectRes);
	    }
	    
	    this.ruleProjectResService.insertBatch(resList);	    
	    
    }

}
